﻿using System;
using System.Net;
using System.IO;
using System.Diagnostics;

namespace WebLayer
{
    /// <summary>
    /// The web-page that logins to speak-up site.
    /// </summary>
    public class LoginPage: WebPage
    {
        private static String CategoryName = "LoginPage";
        private static String LogonFormName = "LOGONFORM";

        public String UserName;
        public String Password;
        public String School;

        public CookieContainer m_cookies = null;
        public CookieContainer Cookies
        {
            get
            {
                if(m_cookies == null)
                {
                    Request();
                }
                return m_cookies;
            }
        }

        static public string LoginPageName = "LoginPage";
        static public Uri LoginPageUri  = GetActionPageUri("login");

        public LoginPage(String userName, String password, String school):
            base(LoginPageName,LoginPageUri)
        {
            this.UserName = userName;
            this.Password = password;
            this.School   = school;
        }

        protected override void OnRequest(HttpWebRequest request)
        {
            base.OnRequest(request);

            Trace.WriteLine(PageName + " - Using user: "+UserName+"; "+
                                            "password: "+Password+"; "+
                                            "school: "  +School   , CategoryName); 

            String dataRequest = "USERNAME=" + UserName + "&" +
                                 "PASS="     + Password + "&" +
                                 "fSzkola="  + School   + "&" ;

            request.CookieContainer = new CookieContainer();            
            
            request.Method          = "POST";
            request.ContentLength   = dataRequest.Length;
            request.ContentType     = "application/x-www-form-urlencoded";

            Stream stream       = null;
            StreamWriter writer = null;
            try
            {
                stream = request.GetRequestStream();
                writer = new StreamWriter(stream);
                writer.Write(dataRequest);                
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
                else if (stream != null)
                {
                    stream.Close();
                }

            }            
        }

        protected override void OnResponse(HttpWebRequest request, HttpWebResponse response)
        {
            base.OnResponse(request, response);
            m_cookies = request.CookieContainer;

            if (PageContent == null || PageContent.IndexOf(LogonFormName) >= 0)
            {
                throw new UnauthorizedAccessException("User/Password/School are invalid.");
            }
        }
    }
}
